package org.csu.jpetstore.controller;

import org.csu.jpetstore.domain.Category;
import org.csu.jpetstore.domain.Item;
import org.csu.jpetstore.domain.Product;
import org.csu.jpetstore.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("catalog/")
public class CatalogController {
    @Autowired
    private CategoryService categoryService;
//
//    @GetMapping("index")
//    public String index() {
//        return "/index";
//    }

    @GetMapping("help")
    public String help(){
        return "help";
    }

    @GetMapping("viewCategory")
    public String viewCategory(String categoryId, Model model) {
        if (categoryId != null) {
            List<Product> productList = categoryService.getProductListByCategory(categoryId);
            Category category = categoryService.getCategory(categoryId);
            model.addAttribute("category", category);
            model.addAttribute("productList", productList);
        }
        return "catalog/Category";
    }

    @GetMapping("viewProduct")
    public String viewProduct(String productId, Model model){
        if(productId != null){
            List<Item> itemList = categoryService.getItemListByProduct(productId);
            Product product = categoryService.getProduct(productId);
            model.addAttribute("product", product);
            model.addAttribute("itemList", itemList);
        }
        return "catalog/Product";
    }

    @GetMapping("viewItem")
    public String viewItem(String itemId, Model model){
        if(itemId != null){
            Item item = categoryService.getItem(itemId);
            Product product = categoryService.getProductByItemId(itemId);
            processProductDescription(product);
            model.addAttribute("product", product);
            model.addAttribute("item", item);
            System.out.println("isInstock");
        }
        return "catalog/Item";
    }

    @GetMapping("searchProducts")
    public void preSearch(HttpServletRequest request,HttpServletResponse response, String keyword) throws ServletException, IOException {
        List<Product> productList = categoryService.searchProductList(keyword);
        response.setContentType("text/xml;charset=utf-8");
        PrintWriter printWriter = response.getWriter();
        printWriter.println("<products>");
        if(productList.size() > 0) {
            for (Product product : productList) {
                printWriter.println("<name>" + product.getName() + "</name>");
            }
        }else{
            printWriter.println("<name>" + "无匹配结果" + "</name>");
        }
        printWriter.println("</products>");
        printWriter.flush();
        printWriter.close();
    }

    @PostMapping("searchProducts")
    public String searchProducts(String keyword, Model model){
        if(keyword != null){
            List<Product> productList = categoryService.searchProductList(keyword.toLowerCase());
            processProductDescription(productList);
            model.addAttribute("productList",productList);
            return "catalog/SearchProducts";
        }
        return "Main";
    }

    private void processProductDescription(Product product){
        String [] temp = product.getDescription().split("\"");
        product.setDescriptionImage(temp[1]);
        product.setDescriptionText(temp[2].substring(1));
    }
    private void processProductDescription(List<Product> productList){
        for(Product product : productList) {
            processProductDescription(product);
        }
    }
    private void presearch(HttpServletRequest request, HttpServletResponse resp){

    }



}
